import { Attestation } from 'attestation-model.js';
import { Config } from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var getUserInfo = require('../../auth/auth.js')
var app = getApp();
var index = new Attestation();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //姓名
    name:'',
    // 性别
    genind:0,
    sex:'',
    user:[],
    gender:[
      {
        oind:1,
        text:'男',
      },
      {
        oind: 2, 
        text: '女'
      }
    ],
    //部门
    sectind:false,
    section:[],
    //职位
    positind:false,
    position: [],
    groupList:[],
    group_index:0,
    group_name:'',
    group_id:0,
    //手机号
    phone:'',
    disabled:false,
  },

  //输入姓名
  bindName(e){
   
    this.setData({
      name: e.detail.value
    })
  },

  // 选择性别
  bindGender:function(e){
    this.setData({
      genind:e.detail.value,
    })
  },

  //选择部门
  bindSection: function (e) {
    this.setData({
      sectind: e.detail.value
    })
  },

  //选择职位
  bindPosition: function (e) {
    this.setData({
      positind: e.detail.value
    })
  },
  bindGroup: function (e) {
    this.setData({
      group_index: e.detail.value,
      group_id:this.data.groupList[e.detail.value].id,
      group_name:this.data.groupList[e.detail.value].name
    })
  },

  //输入手机号
  bindPhone(e){
    this.setData({
      phone: e.detail.value
    })
  },

  //获取
  bindUserInfo() {
    index.getUserInfo({}, (res) => {
      this.setData({
        section:res.data.department,
        position:res.data.position,
        groupList:res.data.group
      })
      if(res.data.user.post_name){
        if (res.data.user.post_status !== 3 ){
          this.setData({
            user: res.data.user,
            disabled:true,
          })
        }
      }
    })
  },


  //保存认证信息
  bindHold(){
    if (!this.data.name){
      Function.layer('请输入姓名')
    } else if (!this.data.genind){
      Function.layer('请选择性别')
    } else if (!this.data.sectind){
      Function.layer('请选部门')
    } else if (!this.data.positind){
      Function.layer('请选职位')
    } else if (!this.data.group_id){
      Function.layer('请选考勤组')
    }else if (!this.data.phone){
      Function.layer('请输入手机号')
    } else{
      if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
        Function.layer('手机号码有误')
        return false;
      } else{
        index.getPostInfo({
          post_name: this.data.name,
          post_phone: this.data.phone,
          post_sex: this.data.gender[this.data.genind].oind,
          department_id: this.data.section[this.data.sectind].id,
          department_name: this.data.section[this.data.sectind].name,
          position_id: this.data.position[this.data.positind].id,
          position_name: this.data.position[this.data.positind].name,
          group_id:this.data.group_id
        },(res)=>{
            if(res.code == 1){
              wx.showToast({
                title: '提交成功',
                icon: 'success',
                duration: 2000
              })
              setTimeout(res => {
                wx.reLaunch({
                  url: '/pages/home/homeIndex/homeIndex'
                })}, 2000) 
            }
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.setData({
      isLogin: app.checkIsLogin()
    });
    if (_this.data.isLogin === false) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.bindUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})